package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppSuccessExample;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppSuccessExampleMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppSuccessExampleService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.successExample.ExampleRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.successExample.ExampleByIdVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.successExample.ExampleVo;
import com.syni.mdd.sjb.common.datatreaure.mybatis.MybatisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 成功案例记录表 服务实现类
 * </p>
 *
 * @author cfx
 * @since 2020-04-23
 */
@Service
public class DxshAppSuccessExampleServiceImpl extends ServiceImpl<DxshAppSuccessExampleMapper, DxshAppSuccessExample> implements DxshAppSuccessExampleService {
    @Autowired
    DxshAppSuccessExampleMapper dxshAppSuccessExampleMapper;
    @Autowired
    DxshAppSuccessExampleService dxshAppSuccessExampleService;

    @Override
    public Map<String, Object> example(ExampleVo exampleVo) {
        Integer pageSize = exampleVo.getPageSize();
        Integer pageNum = exampleVo.getPageNum();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc(DxshAppSuccessExample.NEW_TIME);
        queryWrapper.eq(DxshAppSuccessExample.IS_OFF, Constants.COMMON_FLAG_FALSE_INT);
        Page<DxshAppSuccessExample> page = new Page<>(pageNum,pageSize);
        IPage<DxshAppSuccessExample> dxshAppSuccessExamples = dxshAppSuccessExampleMapper.selectPage(page,queryWrapper);
        Map<String,Object> userData = MybatisUtils.UserData(dxshAppSuccessExamples);
        List<DxshAppSuccessExample> successExamples = dxshAppSuccessExamples.getRecords();
        List<ExampleRpVo> exampleRpVos = BeanUtils.copyList(successExamples,ExampleRpVo.class);
        return ResultMapHelper.success(exampleRpVos,userData, ResultMsg.MSG_GET_SUCCESS);
    }

    public List<ExampleRpVo> serviceCase(Integer pageNum,Integer pageSize){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc(DxshAppSuccessExample.NEW_TIME);
        queryWrapper.eq(DxshAppSuccessExample.IS_OFF, Constants.COMMON_FLAG_FALSE_INT);
        Page<DxshAppSuccessExample> page = new Page<>(pageNum,pageSize);
        IPage<DxshAppSuccessExample> dxshAppSuccessExamples = dxshAppSuccessExampleMapper.selectPage(page,queryWrapper);
        List<DxshAppSuccessExample> successExamples = dxshAppSuccessExamples.getRecords();
        List<ExampleRpVo> exampleRpVos = BeanUtils.copyList(successExamples,ExampleRpVo.class);
        return exampleRpVos;
    }

    @Override
    public Map<String, Object> exampleById(ExampleByIdVo exampleByIdVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppSuccessExample.ID,exampleByIdVo.getExampleId());
        DxshAppSuccessExample dxshAppSuccessExample = dxshAppSuccessExampleMapper.selectOne(queryWrapper);
        if(dxshAppSuccessExample == null){
            return ResultMapHelper.failure(ResultMsg.CODE_ERROR_05);
        }
        //阅读量加一并保存
        dxshAppSuccessExample.setViewNum(dxshAppSuccessExample.getViewNum()+1);
        dxshAppSuccessExampleService.saveOrUpdate(dxshAppSuccessExample);
        return ResultMapHelper.success(dxshAppSuccessExample,ResultMsg.MSG_GET_SUCCESS);
    }
}
